home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 773 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.2 KB

  1. From: clamage@Eng.Sun.COM (Steve Clamage)
  2. Message-ID: <4ikkbu$b5u@engnews1.Eng.Sun.COM>
  3. X-Original-Date: 18 Mar 1996 21:25:18 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 19 Mar 96 00:15:53 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Two questions about declarations in condit
  9. Organization: Sun Microsystems Inc.
  10. References: <sdouglas-1403961511110001@193.131.176.202>
  11. Reply-To: clamage@Eng.Sun.COM
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMU39E+EDnX0m9pzZAQEZWwF+K8qE2YkbTvCe1mg+j4WgcD5mnkJdYySj
  14.     Vzgk7Pjsv+FO+sBNGVf+x2hcS06Eto4r
  15.     =BWJI
  16.  
  17. In article 1403961511110001@193.131.176.202, sdouglas@armltd.co.uk
  18. (scott douglass) writes:
  19.  
  20. >I read section 6.4 carefully but I couldn't decide what the lifetime (not
  21. >scope) of an object declared in the condition of a while or for statements
  22. >is.  Reading D&E 3.11.5.2 didn't help either.  Given the following:
  23.  
  24. >struct T { T(int); ~T(); operator bool() const; /*...*/ };
  25. >
  26. >void f(int i)
  27. >    {
  28. >    while (T t = i) { /* do something with 't' */ }
  29. >    }
  30.  
  31. >There are two "obvious" possibilities, I9m leaning toward the first:
  32.  
  33. >1 -- The object is initialized just once and destroyed just once, making
  34. >'f' above eqivalent to:
  35.  
  36. >void f(int i)
  37. >    {
  38. >        {
  39. >        T t = i;
  40. >        while (t) { /* do something with 't' */ }
  41. >        }
  42. >    }
  43.  
  44. Interesting question, and I believe your preferred interpretation is
  45. correct. Here's why: It is clear from the definition of "while" that
  46. the controlled statements that are iterated are those which follow the
  47. condition; the condition is not part of the iterated statement or block.
  48. Further, the *value* of the condition determines whether the controlled
  49. statements are executed. The condition is not a statement, and so
  50. only its value is recomputed; the definition is not re-executed.
  51.  
  52. Besides, it would be rather pointless if in your example 't' was set to
  53. 'i' each time around the loop.
  54.  
  55.  
  56. >Bonus question:  why does the grammer allow only the '=
  57. >assignment-expression' form:
  58.  
  59. >          condition:
  60. >                  expression
  61. >                  type-specifier-seq declarator = assignment-expression
  62.  
  63. >instead of:
  64.  
  65. >          condition:
  66. >                  expression
  67. >                  type-specifier-seq declarator = assignment-expression
  68. >                  type-specifier-seq declarator ( expression-list )
  69.  
  70. >So that I could write:
  71.  
  72. >void f(int i)
  73. >    {
  74. >    while (T t(i)) { /* do something with 't' */ }
  75. >    }
  76.  
  77. I think the general case could lead to ambiguities in parsing. If a
  78. function call can possibly be interpreted as a variable definition, then
  79. it is a variable definition. That could lead to unexpected behavior
  80. which would be difficult to track down.
  81.  
  82. You can always write the same thing with "=" notation:
  83.     while( T t = T(i) ) { ... }
  84. This rewrite is not guaranteed to be as efficient, but most compilers
  85. do optimize away the extra copy.
  86. ---
  87. Steve Clamage, stephen.clamage@eng.sun.com
  88. ---
  89. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  90. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  91. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  92. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  93. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  94.